<template>
  <div class="container">
    <baleTable
      :tableData="tableData"
      :tableHead="tableHead"
      :isSearch="true"
      :tableLoading="tableLoading"
      :page="page"
      @pageSizeChange="pageSizeChange"
      @handleCurrentChange="handleCurrentChange"
      @searchMethods="searchMethods"
      @tableSortChange="tableSortChange"
      @handleRowClick="handleRowClick"
      ref="baleTable"
    >
      <template #headLeft>
        <!--   <el-button size="small" type="success" @click="importData()"
          >数据导入</el-button
        > -->
        <el-button size="small" type="primary" @click="showDialog('')"
          >添加</el-button
        >
        <!-- <div
          class="ml10 tableStats-btn tableStats-btn-active"
          @click="showDialog('')"
        >
          添加
        </div> -->
      </template>
      <template #icon="{ scope }">
        <div @click.stop="">
          <tablePrImg
            v-if="scope.row.icon"
            :imgSrc="scope.row.icon"
            :prImgList="[scope.row.icon]"
          />
        </div>
      </template>
      <template #role_id="{ scope }">
        {{ scope.row.role && scope.row.role.display_name }}
      </template>
      <!-- 等级 -->
      <template #level="{ scope }">
        <el-tag v-if="scope.row.level == '高级'" type="danger">{{
          scope.row.level
        }}</el-tag>
        <el-tag v-if="scope.row.level == '中级'" type="warning">{{
          scope.row.level
        }}</el-tag>
        <el-tag v-if="scope.row.level == '普通'" type="info">{{
          scope.row.level
        }}</el-tag>
      </template>

      <!--    操作 -->
      <template #tableOperation="{ scope }">
        <!--   <el-button type="danger" size="small" @click="userList(scope.row.id)"
          >用户订单列表</el-button
        > -->
        <!--  <el-button type="info" size="small" @click="addQuality(scope.row.id)"
          >资质申请列表</el-button
        > -->
        <!--    <el-button
          type="success"
          size="small"
          @click="resetButton(scope.row.id)"
          icon="el-icon-key"
          >重置密码</el-button
        > -->
        <el-button type="primary" size="small" @click="showDialog(scope.row)"
          >编 辑</el-button
        >
        <el-button type="danger" size="small" @click="deList(scope.row.id)"
          >删除</el-button
        >
      </template>
    </baleTable>

    <menuDialog
      ref="dialognur"
      :roleData="roleData"
      @refresh="getTableData"
    ></menuDialog>

    <!--   <userList ref="userListDialog"></userList> -->
  </div>
</template>

<script>
import {
  Apirole,
  ApiAdminuser,
  ApiDelAdminuser,
  ApiResetPassword,
} from "@a/index";
import { tableHead } from "./config";
import menuDialog from "./component/basicDialog.vue";
//获取用户列表数据
/* import userList from "./component/userList.vue"; */
import tablePrImg from "@/components/tablePrImg.vue";

export default {
  components: {
    menuDialog,
    tablePrImg,

    /*   userList, */
  },
  data() {
    return {
      tableHead,
      tableData: [],
      page: {
        page: 1,
        size: 25,
        total: 0,
      },
      tableLoading: false,
      searchParams: {},
      sortParams: {},
      editFormVisible: false,
      btnLoading: false,
      roleData: [],
    };
  },
  mounted() {
    this.getTableData();
    this.getOtherData();
  },
  methods: {
    getOtherData() {
      Apirole({ page: 1, size: 999 }).then((res) => {
        this.roleData = res.data;
        const index = this.tableHead.findIndex(
          (item) => item.prop === "role_id"
        );
        if (index >= 0) {
          this.tableHead[index].searchOptions = res.data;
        }
      });
    },

    importData() {
      this.$refs.importData.show();
    },
    //审核资质
    /*   addQuality(id) {
      this.$router.push({
        name: "quDetail",
        params: { id },
      });
    }, */

    //编辑界面
    onEdit() {},
    showDialog(val) {
      this.$refs.dialognur.show(val);
    },
    /*    userList(id) {
      this.$router.push({
        name: "detail",
        params: { id },
      });
    }, */
    getTableData(params = {}) {
      let postParams = {
        ...params,
        ...this.searchParams,
        ...this.sortParams,
        ...this.page,
      };
      this.tableLoading = true;
      ApiAdminuser(postParams).then((res) => {
        let { data, page } = res;
        this.tableData = data;
        this.page.total = page.total;
        this.tableLoading = false;
      });
    },
    searchMethods(params) {
      this.searchParams = {};
      this.page.page = 1;
      this.searchParams = params;
      this.getTableData();
    },
    pageSizeChange(val) {
      this.page.size = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.page = val;
      this.getTableData();
    },
    tableSortChange(params) {
      if (params.sort_str) {
        this.sortParams = { ...this.sortParams, ...params };
      } else {
        delete this.sortParams.sort_str;
        delete this.sortParams.sort_order;
      }
      this.page.page = 1;
      this.getTableData();
    },
    handleRowClick(params) {},
    //shanchu
    deList(id) {
      this.$confirm("是否删除该数据项?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "warning",
      })
        .then((res) => {
          ApiDelAdminuser(id).then((res) => {
            this.$message.success("操作成功！");
            this.getTableData();
          });
        })
        .catch((err) => {});
    },
    resetButton(id) {
      this.$confirm("是否确认重置密码?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "warning",
      })
        .then((res) => {
          ApiResetPassword(id).then((res) => {
            this.$alert("重置成功，该员工初始密码为：123456", "提示", {
              confirmButtonText: "确定",
              callback: (action) => {},
            });
            this.getTableData();
          });
        })
        .catch((err) => {});
    },
  },
  filters: {},
};
</script>

<style lang="less" scoped></style>
